<?php
/**
 * Bdat Site development
 * @author Will Skates
 */

/*----------------------------------------------------------------------------------------------------*/

/**
 * Outline of properties and functionality required to manage aggregated showcase data within the system.
 * @author Will Skates
 */
class ShowcaseAdmin extends CI_Model {
	
	/*
	 * List of categories in which list items can appear.
	 */
	public $listModes = array(
		array('canapprove',2),
		array('canpublish',3),
		array('cansticky',3),
		array('aresticky',3)
	);
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/*
	 * Descriptions of those categories ($listModes).
	 */
	public $titles = array(
		'canapprove'=>'Waiting to be approved',
		'canpublish'=>'Approved but not published',
		'cansticky'=>'Published but not stickied',
		'aresticky'=>'Published and stickied'
	);
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/*
	 * Queries which put content into those categories (one for each).
	 */
	public $listWheres = array(
		'approved = 0 AND published = 0 AND sticky = 0',
		'approved = 1 AND published = 0 AND sticky = 0',
		'approved = 1 AND published = 1 AND sticky = 0',
		'approved = 1 AND published = 1 AND sticky = 1'
	);
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/*
	 * An array linking a process or an action on a piece of content, to the relevant query.
	 */
	public $routes = array(
		'approve'=>0,
		'unapprove'=>1,
		'publish'=>2,
		'unpublish'=>1,
		'sticky'=>3,
		'unSticky'=>2
	);

	/*----------------------------------------------------------------------------------------------------*/
	
	/*
	 * Headings for each of the categories which outline what functionality is available on that piece of content.
	 */
	public $listHeadings = array(
		array('Approve this post'),
		array('Unapprove this post', 'Publish this post'),
		array('Unpublish this post', 'Sticky this post'),
		array('UnSticky this post')
	);
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/*
	 * Lists of url's where those actions can be performed. 
	 */
	public $listLinks = array(
		array('show/showcase/approve'),
		array('show/showcase/unapprove','show/showcase/publish'),
		array('show/showcase/unpublish','show/showcase/sticky'),
		array('show/showcase/unsticky')
	);
	
	/*----------------------------------------------------------------------------------------------------*/
	
	//What to call this content type (as it has a custom cms).
	public $referenceName = 'Showcase';
	
	//The current mode that the page is on.
	public $currentMode = false;
	public $roleRequired;
	
	//Where to find data relating to this content type.
	public $ctable = 'showcase';
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Launch this custom content admin system.
	 * @param 	Array	$args 	- A list of arguments passed to this class via the url.
	 * @return	Void
	 */
	public function launch($args)
	{
		
		//If the url doesn't tell us which mode to use then go to the first one.
		if (!isset($args[0])) {
			$args[0] = 'canapprove';
		}
		
		//Set the current mode from the arguments in the url.
		$this->setCurrentMode($args);
		
		if ( $this->currentMode !== false ) {
			
			
			//Check that the current user has sufficient access.
			$this->roleRequired = $this->listModes[$this->currentMode][1];
			$hasAccess = admin::$currentUser->userHasAccess($this->roleRequired);
			if($hasAccess) {
				
				$links = array();
				foreach ($this->listModes as $k=>$v) {
					$links[] = array(
						'href'=>'other/show/' . $this->referenceName  . '/' . $v[0],
						'text'=>$this->titles[$v[0]]
					);
				}
				
				$this->load->View('admin/custom/custommenu',array('links'=>$links));
				$result = $this->db->query($this->makeListQuery())->result();
				
				
				
				
				
				if(count($result) > 0) {
					$this->drawList($result);
				}
				
				
		
			} else {
				echo 'You do not have sufficient access to view this list';
			}
		} else {
			//check to make sure that we dont need to do anything else.
			
			if ( method_exists($this, $args[0]) ) {
				
				if ( isset($args[1]) ) {
					$name = $args[0];
					$this->load->Model($this->referenceName);
					$ref = $this->referenceName;
					$this->$ref = $this->$ref->makeFromID($args[1]);
					$this->$name();
				} else {
					echo 'you need to pass the id of an item to do that.';
				}
				
			} else {
				echo 'That functionality does not exist';
			}
		}

	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Approve a piece of content to be reviewed by a moderator.
	 * @return	Void
	 */
	public function approve()
	{
		$ref = $this->referenceName;
		$this->$ref->approved(true);
		$this->redirect('approve');
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Dis-Approve a piece of content to be reviewed by a moderator.
	 * @return	Void
	 */
	public function unapprove()
	{
		$ref = $this->referenceName;
		$this->$ref->approved(false);
		$this->redirect('unapprove');
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Make a piece of content visible on the homepage.
	 * @return	Void
	 */
	public function publish()
	{
		$ref = $this->referenceName;
		$this->$ref->published(true);
		$this->redirect('publish');
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Stop a piece of content from being viewable on the homepage.
	 * @return	Void
	 */
	public function unpublish()
	{
		$ref = $this->referenceName;
		$this->$ref->published(false);
		$this->redirect('unpublish');
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Place a piece of content at the top of the list on the homepage.
	 * @return	Void
	 */
	public function sticky()
	{
		$ref = $this->referenceName;
		$this->$ref->stickied(true);
		$this->redirect('sticky');
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Remove a piece of content from the top of the list on the homepage.
	 * @return	Void
	 */
	public function unSticky()
	{
		$ref = $this->referenceName;
		$this->$ref->stickied(false);
		$this->redirect('unSticky');
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Redirect a user to the correct page based on the action that they want to perform.
	 * @param	String	$name	-	A string which relates to a key in the $routes array.
	 * @return	Void
	 */
	public function redirect($name)
	{
		$num = $this->routes[$name];
		header('Location: /i-dat.org/admin/other/show/' . $this->referenceName . '/' . $this->listModes[$num][0] . '/');exit;
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Draw a list of all the content that suits the current category being viewed.
	 * @param	Array	$result		-	A list of all of the items to go into the list.
	 * @param 	Array 	$fields		- 	(Optional) A list odf field names to use as the headings for the table.
	 * @return	Void
	 */
	public function drawList($result, $fields = array())
	{
		
		$this->load->library('table');
		
		$headings = array_keys((Array)$result[0]);
		
		if ( $fields === array() ) {
			$fields = $headings;
		}
		
		$headings = array_merge($headings,$this->listHeadings[$this->currentMode]);
		
		$rows = array();
		$rows[] = $headings;
		
		foreach ( $result as $key=>$value ) {
			$row = array();
			
			foreach ($value as $rowkey=>$val) {
				if ( in_array($rowkey, $fields) ) {
					$row[] = strip_tags($val);
				}
			}

			foreach ($this->listLinks[$this->currentMode] as $k=>$link) {
				$row[] = '<a href="/i-dat.org/admin/other/' . $link . '/' . $value->uid . '">' . $this->listHeadings[$this->currentMode][$k] . '</a>';
			}
			
			$rows[] = $row;
			
		}
		
		$this->load->View('admin/pageView',array('content'=>$this->table->generate($rows)));
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Make an sql query to get all of the data for the current category being viewed.
	 * @return	String	$query	-	The query.
	 */
	public function makeListQuery()
	{
		$query = 'SELECT 
						showcase.uid,
						author,
						title,
						content,
						timestamp,
						showcase.link,
						image,
						user_profiles.name,
						user_profiles.link,
						gradyears.date AS gradyear
				 FROM ' . $this->ctable . '
				 LEFT JOIN user_profiles ON user_profiles.userid = ' . $this->ctable . '.author
				 LEFT JOIN gradyears ON gradyears.uid = user_profiles.gradyear
				 WHERE ' . $this->listWheres[$this->currentMode];
		return $query;
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Detect what category is currently being viewed from arguments in the url of the page.
	 * @param	Array	$args	- A list of arguments from the url.
	 * @return	Void.
	 */
	public function setCurrentMode($args)
	{
		switch($args[0]) {
			case 'Canpublish':
			case 'canpublish':
				$this->currentMode =1;
			break;
			case 'cansticky':
			case 'Cansticky':
				$this->currentMode =2;
			break;
			case 'Aresticky':
			case 'aresticky':
				$this->currentMode =3;
			break;
			case 'canapprove':
			case 'CanApprove':
				$this->currentMode =0;
			break;
		}
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
}